package com.yin.myproject.mq.client;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.ResourceBundle;

/**
 * Created by Eason on 2017/6/22.
 */
public class CreateConnectionSample {

    private final ResourceBundle bundle = ResourceBundle.getBundle("mq");

    public Connection createConnByUri() {
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setUri("amqp://userName:password@hostName:portNumber/virtualHost");
            Connection conn = factory.newConnection();
            return conn;
        } catch (Exception e) {
            return null;
        }
    }

    public Connection createConn() {
        Connection conn = null;
        String userName = bundle.getString("userName");
        String password = bundle.getString("password");
        String hostName = bundle.getString("hostName");
        Integer portNumber = Integer.parseInt(bundle.getString("portNumber"));
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(userName);
        factory.setPassword(password);
        factory.setHost(hostName);
        factory.setPort(portNumber);
        try {
            conn = factory.newConnection();
        } catch (Exception e) {

        }
        return conn;
    }
}
